package vip.liux.gateway.filters.decrypt;

import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 基于配置文件加载，启动后常驻本地内存的 appId 公私钥仓储实现。
 * 依赖 AppKeyMemoryProperties 自动注入配置。
 */
public class AppKeyMemoryRepository implements AppKeyRepository {

    private final Map<String, KeyPairConfig> keyCache = new ConcurrentHashMap<>();

    public AppKeyMemoryRepository(AppKeyRepositoryProperties appKeyRepositoryProperties) {
        if (appKeyRepositoryProperties != null && appKeyRepositoryProperties.getApps() != null) {
            for (Map.Entry<String, KeyPairConfig> entry : appKeyRepositoryProperties.getApps().entrySet()) {
                String appId = entry.getKey();
                keyCache.put(appId, entry.getValue());
            }
        }
    }

    @Override
    public Mono<KeyPairConfig> getKeyPairConfig(String appId) {
        KeyPairConfig config = keyCache.get(appId);
        return config != null ? Mono.just(config) : Mono.empty();
    }
}